import { PackageTableItem, PackageTableParams, UpgradePackageParams } from './model/upgradePackageModel';
import { tenantHttp } from '/@/utils/http/axios';
import dayjs from 'dayjs';
enum Api {
  UPGRADE_PACKAGE = '/tenantApp/subPackage/upgradeTenantPackage',
  TABLE = '/tenantApp/subPackage/getPackage',
}

export const upgradePackage = async (params: UpgradePackageParams) => {
  const res = await tenantHttp.get<boolean>({
    url: Api.UPGRADE_PACKAGE,
    params,
  });
  return res;
};

export const getPackageTable = async (params: PackageTableParams) => {
  const res = await tenantHttp.post<PackageTableItem>({
    url: Api.TABLE,
    params,
  });

  res.items = res.items.map((item) => {
    if (item.status == 'normal') {
      item.status = '正常（使用中）';
    } else if (item.status == 'underReview') {
      item.status = '审核中';
    } else if (item.status == 'freeze') {
      item.status = '冻结';
    } else if (item.status == 'deploying') {
      item.status = '部署中';
    } else if (item.status == 'fail') {
      item.status = '部署失败';
    } else if (item.status == 'rejected') {
      item.status = '审核未通过';
    }
    item.enabledStr = item.enabled == true ? '是' : '否';
    if (item.expireTime !== null) {
      item.expireTime = dayjs(item.expireTime).format('YYYY-MM-DD HH:mm:ss');
    }
    if (item.effectiveTime !== null) {
      item.effectiveTime = dayjs(item.effectiveTime).format('YYYY-MM-DD HH:mm:ss');
    } else {
      item.effectiveTime = '--';
    }
    if (item.type === null) {
      item.type = '--';
    }
    return item;
  });

  return res;
};
